// pages/openBookcase/onOpen/onOpen.js
const request = require('../../../utils/request.js');
//获取应用实例
const app = getApp()
var tempBook;
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
  },
  onShow: function () {
    console.log("onshow---")
    var that = this;
    that.getToken();
  },
  getToken: function () {
    var that = this;
    // 获取本地存储中的token
    wx.getStorage({
      key: 'token',
      success: function (res) {//有该数据
        console.log(res.data)
        that.setData({
          token: res.data
        })
        var tempToken = res.data;
        request.getUid({
          'session_id': tempToken
        },
          (res) => {
            console.log(res)
            that.setData({
              uid: res.data.data.id
            })

            that.testSocket();
          })
        request.getIsOpenDoor({
          'session_id': tempToken
        },
          (res) => {
            console.log(res)
            if (res.data.status == "error") {
              wx.redirectTo({
                url: '../onClose/onClose',
              })
            }
          })

      },
      fail: function (res) {
        console.log(res.data)
        //调用授权方法-设置token
        app.getUserInfo();
      }
    })
  },

  //测试socket
  testSocket: function () {
    var that = this;
    wx.connectSocket({
      url: "wss://www.qiyueduba.com:9899",
      success: function () {
        console.log('连接成功')
      }
    })
    wx.onSocketOpen(function (res) {
      var data1 = '{"uid":' + that.data.uid + ',"type":"connect"}'
      console.log('WebSocket连接已打开！')
      wx.sendSocketMessage({
        data: data1,
        success: function (res) {
          console.log(res)
        }
      })

      wx.onSocketMessage(function (res) {
        console.log('收到服务器内容：' + res.data)

        if (JSON.parse(res.data).close_door == 'ok') {
          wx.closeSocket({
            success: function (res) {
              console.log(res)
              wx.redirectTo({
                url: '../onClose/onClose',
              })
            }
          })
        }

        if (typeof JSON.parse(res.data) == "object") {
          if (JSON.parse(res.data).connect == 'ok') {
            console.log("连接成功")
          } else {
            console.log("执行了")
          }
        }


      })

      wx.onSocketClose(function (res) {
        console.log('WebSocket 已关闭！')
        // that.testSocket()
        wx.redirectTo({
          url: '../onClose/onClose',
        })
      })

    })
  },
  //goFAQ
  goFAQ: function () {
    wx.navigateTo({
      url: '../../my/my-faq/my-faq',
    })
  },
  gotoindex: function () {
    console.log(getCurrentPages())
    wx.navigateBack({
      delta: 1
    })
  },
  goShowModal: function () {
    wx.showModal({
      title: '借还须知',
      content: '1.最多在借书籍不超过五本！\r\n2.借/还图书完成请关闭柜门。\r\n3.柜门开启后，您可以借阅您需要的书籍，也可以归还书籍，期间所有操作均为本人行为，离开请务必关闭柜门。\r\n4.为方便后续人员借还图书，请您尽量在三分钟内完成借还图书。\r\n5.图书借阅后，请爱惜图书，如产生书籍破损需补偿相关维护费用。',
      showCancel: false,
      confirmColor: "#FF0000",
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          // that.openDoor();

        }
      }
    })
  }
})